$(function(){
	$('#addWin').window('close');  
	$('#updateWin').window('close');  
  	createTree();
  	initUser();  	
});



var p_mid = "000";
var p_name = "部门";
//组织机构属性： 10 公司；20 部门
var p_orgNature = 10;
/**
 * 创建菜单树
 */
function createTree(){
   $("#tree").tree({
      url:'department.do?method=queryDepartment&tid=-1',
      onBeforeExpand:function(node,param){      	 
         $('#tree').tree('options').url = "department.do?method=queryDepartment&tid="+node.id;
      },
      lines : true,
      onClick:function(node){
      	  p_mid = node.id;
      	  p_name = node.text;
      	  p_orgNature = node.orgNature;
//      	  var jsons = {"citysId":node.id}; 
//      	  
//      	  reloadTable({tableId:'users',params:jsons});//刷新列表
      	  search();
      }
   });
}
/**
 * 初始化菜单列表
 */
function initUser(){
	dataTable({
      tableId:'users',
      title:'用户列表',
      url:'userLoginController.do?method=queryUser',
      idField:'id',
      columns:[[
		 {field : 'id',checkbox : true,align : 'center'},
		 {title : '登录名',field : 'loginName',width : 150},
		 {title : '姓名',field : 'userName',width : 100},
		 {title : '组织机构',field : 'city',width:150},
		 {title : '性别',field : 'sex',width : 60,formatter:function(value,row,index){
		    if(value=='1'){
		    	return "<span style='color:red;font-weight:bold;'>男</span><img src='images/icon/user_male.png' />";
		    }else{
		    	return "<span style='color:green;font-weight:bold;'>女</span><img src='images/icon/user_female.png' />";
		    }		    
		 }},
//		 {title : '用户角色',field : 'roleName',width:100},
		 {title : '手机号',field : 'mobilePhone',width:100},
		 {title : '电话',field : 'tel',width:100},
		 {title : '电子邮箱',field : 'email',width:150},
		 {title : '其它联系方式',field : 'otherContactWay',width:300}
		 ]],
	  showPage:true,
	  toolbar:'toolbar'
   });
}
$("#user_add").click(function(){
	$("#addWin").window({
		title:'增加用户信息',
		top:10,
		left:100,
		height:520,
		width:400,   
		content:createFrame('userLoginController.do?method=index&type=add'),
		iconCls:'icon-win-addmenu',
		collapsible:false,	
		 modal:true,
		 disabled:true,
		 minimizable:false,
		 inline:true
	   });
	$('#addWin').window('open');  
	
//	var url = "userLoginController.do?method=index&type=add";	
//	url = urlAddParmert(url,"citysId",p_mid);
//	url = urlAddParmert(url,"citysName",p_name);
//    easyuiWidow({
//    	id:'add',
//   	 title:'增加用户信息',
//     winId:'addWin',
//     left:100,
//     width:450,   
//     height:520,
//     iconCls:'icon-win-addmenu',
//     url:url
//   });
});
//修改数据
$("#user_update").click(function(){
   var rows = $("#users").datagrid("getChecked");
   var len = rows.length;
   if(len<1){
   	  $.messager.alert("系统提示", "请选择要修改的数据！","warning");
   	  return;
   }else if(len>1){
   	  $.messager.alert("系统提示", "每次只能修改一条数据，请重新选择！","warning");
   	  return;
   }
   updateRowData(rows[0].id);   
});
//删除数据
$("#user_delete").click(function(){  
   var rows = $('#users').datagrid('getSelections');
   var len = rows.length;
   if(len==0){
      $.messager.alert("系统提示","请选择要删除的记录","warning");
      return;
   }
   var ids = new Array();
   for(var i = 0;i<len;i++){     
   	  ids.push(rows[i].id);
   }
   deleteRowData(ids);
});
function updateRowData(menuId){  
	
	$("#updateWin").window({
		title:'修改用户信息',
		top:10,
		left:100,
		height:520,
		width:400,   
		content:createFrame('userLoginController.do?method=index&type=update&id='+menuId),
		iconCls:'icon-win-addmenu',
		collapsible:false,	
		 modal:true,
		 disabled:true,
		 minimizable:false,
		 inline:true
	   });
	$('#updateWin').window('open'); 
	
//   var url = "userLoginController.do?method=index&type=update";
//   url = urlAddParmert(url,"id",menuId);   
//   //打开easyui窗口
//   easyuiWidow({
//   	 title:'修改用户信息',
//     winId:'updateWin',
//     width:450,   
//     height:520,
//     iconCls:'icon-win-addmenu',
//     url:url
//   });
}
//删除数据
function deleteRowData(menuIds){   
   var len = 1;
   var ids = new Array();
   if(typeof(menuIds)!='string'){
     len = menuIds.length;
     ids = menuIds;
   }else{
     ids.push(menuIds);
   }
   var url = "userLoginController.do?method=deleteUser";
    //删除数据
   deleteData({
     len:len,
     url:url,
     params:{'id':ids,'version':Math.random()},
     dataType:'text',
     callback:function(msg){
        if(msg=="EMPTY"){
           $.messager.alert("系统提示","请选择要删除的记录","warning");
        }else if(msg=="FAIL"){
           $.messager.alert("系统提示","删除菜单信息失败","warning");
        }else if(msg=="SUCCESS"){
           reloadTable({tableId:'users'});		   
        }else{
            $.messager.alert("系统提示","系统出现异常："+msg,"error");
        }
     }
   });
}
$("#user_upload").click(function(){
   	var url = "pages/sys/user/uploadUser.jsp";   	
   	url = urlAddParmert(url,"version",Math.random());
   easyuiWidow({
   	  title:'导入用户资料',
      winId:'uploadWin',
      width:400,   
      height:200,
      iconCls:'icon-win-addmenu',
      url:url
   });   	
});
$("#user_export").click(function(){
   window.location.href="userLoginController.do?method=exportToExcel&cityId="+p_mid;
});

$("#search").click(function(){
	search();
});

function search(){
	var loginName = $("#loginName").val();
	var userName = $("#userName").val();
	var sex = $("#sex").val();
	var roleId = $("#roleId").val();
	
	var params = 
	 {  
	     "loginName":loginName
	   , "userName":userName
	   , "sex":sex
       , "roleId":roleId
       , "citysId":p_mid
       , "orgNature":p_orgNature
    };	
	reloadTable({tableId:'users',params:params});
}


function initCity(cityName){
	   $("#citysId").combotree({
	   	  width:140,
	      url:'department.do?method=queryDepartment&tid=-1',
	      onBeforeExpand:function(node,param){      	 
	         $('#citysId').combotree('tree').tree('options').url = "department.do?method=queryDepartment&type=user&tid="+node.id;
	      },
	      onLoadSuccess:function(node,data){
	        $('#citysId').combotree('setText', cityName);
	      }
	   });
	}


